home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / Text / Statistics.php < prev    next >
PHP Script  |  2004-03-24  |  5KB  |  168 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 softtabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩|
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2002 The PHP Group╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩|
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license,╩╩╩╩╩╩╩|
  9. // | that is bundled with this package in the file LICENSE, and is╩╩╩╩╩╩╩╩|
  10. // | available at through the world-wide-web at╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩|
  11. // | http://www.php.net/license/2_02.txt.╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩|
  12. // | If you did not receive a copy of the PHP license and are unable to╩╩╩|
  13. // | obtain it through the world-wide-web, please send a note to╩╩╩╩╩╩╩╩╩╩|
  14. // | license@php.net so we can mail you a copy immediately.╩╩╩╩╩╩╩╩╩╩╩╩╩╩╩|
  15. // +----------------------------------------------------------------------+
  16. // | Author: George Schlossnagle <george@omniti.com>                      |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id$
  20.  
  21. /*
  22.  * Text_Statistics calculates some basic readability metrics on a 
  23.  * block of text.  The number of words, the number of sentences,
  24.  * and the number of total syllables is counted.  These statistics
  25.  * can be used to calculate the Flesch score for a sentence, which
  26.  * is  a number (usually between 0 and 100) that represents the 
  27.  * readability of the text.  A basic breakdown of scores is:
  28.  *
  29.  * 90 to 100  5th grade
  30.  * 80 to 90   6th grade
  31.  * 70 to 80   7th grade
  32.  * 60 to 70   8th and 9th grade
  33.  * 50 to 60   10th to 12th grade (high school)
  34.  * 30 to 50   college
  35.  * 0 to 30    college graduate
  36.  *
  37.  * More info can be read up on at 
  38.  * http://www.mang.canterbury.ac.nz/courseinfo/AcademicWriting/Flesch.htm
  39.  *
  40.  * require 'Text/Statistics.php';
  41.  * $block = Text_Statistics($sometext);
  42.  * $block->flesch; // returns flesch score for $sometext
  43.  *
  44.  * see the unit tests for additional examples.
  45.  *
  46.  * @package Text_Statistics
  47.  * @author  George Schlossnagle <george@omniti.com>
  48.  */
  49.  
  50. require_once "Text/Word.php";
  51.  
  52. class Text_Statistics {
  53.     /*
  54.      * The document text.
  55.      *
  56.      * @var string
  57.      * @access public
  58.      */
  59.     var $text = '';
  60.  
  61.     /*
  62.      * The number of syllables in the document.
  63.      *
  64.      * @var number
  65.      * @access public
  66.      */
  67.     var $numSyllables = 0;
  68.  
  69.     /*
  70.      * The number of words in the document.
  71.      *
  72.      * @var number
  73.      * @access public
  74.      */
  75.     var $numWords = 0;
  76.  
  77.     /*
  78.      * The number of unique words in the document.
  79.      *
  80.      * @var number
  81.      * @access public
  82.      */
  83.     var $uniqWords = 0;
  84.  
  85.     /*
  86.      * The number of sentences in the document.
  87.      *
  88.      * @var number
  89.      * @access public
  90.      */
  91.     var $numSentences = 0;
  92.  
  93.     /*
  94.      * The Flesch score of the document.
  95.      *
  96.      * @var number
  97.      * @access public
  98.      */
  99.     var $flesch = 0;
  100.  
  101.     /*
  102.      * Some abbreviations we should expand.  THis list could/should
  103.      * be much larger.
  104.      *
  105.      * @var number
  106.      * @access protected
  107.      */
  108.     var $_abbreviations = array('/Mr\./'   => 'Misterr',
  109.                                 '/Mrs\./i' => 'Misses', // Phonetic
  110.                                 '/etc\./i' => 'etcetera',
  111.                                 '/Dr\./i'  => 'Doctor',
  112.                                );
  113.  
  114.     /*
  115.      * Constructor.
  116.      *
  117.      * @param string
  118.      * @access public
  119.      */
  120.     function Text_Statistics($block) 
  121.     {
  122.         $this->text = $block;
  123.         $this->_analyze();
  124.     }
  125.  
  126.     /*
  127.      * Compute statistics for the document object.
  128.      *
  129.      * @access protected
  130.      */
  131.     function _analyze() 
  132.     {
  133.         $lines = explode("\n", $this->text);
  134.         foreach( $lines as $line ) {
  135.             $this->_analyze_line($line);
  136.         }
  137.         $this->flesch = 206.835 - 
  138.             (1.015 * ($this->numWords/$this->numSentences)) -
  139.             (84.6 * ($this->numSyllables/$this->numWords));
  140.     } 
  141.  
  142.     /*
  143.      * Helper function, computes statistics on a given line.
  144.      *
  145.      * @param string
  146.      * @access protected
  147.      */
  148.     function _analyze_line($line) 
  149.     {
  150.         // expand abbreviations for counting syllables
  151.         $line = preg_replace(array_keys($this->_abbreviations), 
  152.                  array_values($this->_abbreviations),
  153.                  $line);
  154.         preg_match_all("/\b(\w[\w'-]*)\b/", $line, $words);
  155.         foreach( $words[1] as $word ) {
  156.             $w_obj = new Text_Word($word);
  157.             $this->numSyllables += $w_obj->numSyllables();
  158.             $this->numWords++;
  159.             if($this->_uniques[strtolower($word)]++ == 0) {
  160.                $this->uniqWords++;
  161.             }
  162.         }
  163.         preg_match_all("/[.!?]/", $line, $matches);
  164.         $this->numSentences += count($matches[0]);
  165.     }
  166. }
  167. ?>
  168.